package com.tangyee.person_risk_assessment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.tangyee.R;
import com.tangyee.person_risk_assessment.dao.AssessResultDAO;

/**
 * Created By Yu Huawen On 2018/11/30
 */
public class RiskAssessmentResultActivity extends AppCompatActivity {

    final private String tableName = "AssessResult";

    private AssessResultDAO assessResultDAO;

    private View neverAssess;
    private View highRiskState;
    private View lowRiskState;
    private TextView riskValue;
    private TextView riskFactor;

    // 在布局里注册过
    public void startRiskAssess(View view){
        Intent intent = new Intent(this, StartRiskAssessmentActivity.class);
        startActivity(intent);
    }

    public void backPersonCenter(View view){
        super.onBackPressed();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        loadWhichView();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_risk_assessment_result);

        init();
    }

    private void init(){
        assessResultDAO = new AssessResultDAO(this);

        // 根据数据里的数据（用户上一次评估的结果），来选择该加载哪一个View
        loadWhichView();
    }


    private void loadWhichView(){
        int count = assessResultDAO.getRecordNumber(tableName);
        int sumScore = getRiskValue();
        System.out.println("count " + count);
        if (count > 0){ // 用户有过评估记录
            if(sumScore >=0 && sumScore < 5) { // 评估结果为低风险
                neverAssess = findViewById(R.id.never_assess);
                highRiskState = findViewById(R.id.high_risk_state);
                lowRiskState = findViewById(R.id.low_risk_state);
                neverAssess.setVisibility(View.GONE);
                highRiskState.setVisibility(View.GONE);
                lowRiskState.setVisibility(View.VISIBLE);

                riskValue = lowRiskState.findViewById(R.id.low_risk_value);
                riskFactor = lowRiskState.findViewById(R.id.low_factor);
                riskValue.setText(sumScore + "");
                riskFactor.setText(getRiskFactor(sumScore));
            }else { // 评估结果为高风险
                neverAssess = findViewById(R.id.never_assess);
                highRiskState = findViewById(R.id.high_risk_state);
                lowRiskState = findViewById(R.id.low_risk_state);
                neverAssess.setVisibility(View.GONE);
                highRiskState.setVisibility(View.VISIBLE);
                lowRiskState.setVisibility(View.GONE);

                riskValue = findViewById(R.id.high_risk_value);
                riskFactor = findViewById(R.id.high_factor);
                riskValue.setText(sumScore + "");
                riskFactor.setText(getRiskFactor(sumScore));
            }
        }else { // 用户没有评估过
            neverAssess = findViewById(R.id.never_assess);
            highRiskState = findViewById(R.id.high_risk_state);
            lowRiskState = findViewById(R.id.low_risk_state);
            neverAssess.setVisibility(View.VISIBLE);
            highRiskState.setVisibility(View.GONE);
            lowRiskState.setVisibility(View.GONE);
        }

    }

    private int getRiskValue(){
        int sumScore = 0;

        int[] scores =  assessResultDAO.getQuestionScore(tableName);
        for (int score : scores){
            sumScore += score;
        }

        return sumScore;
    }

    private String getRiskFactor(int sumScore){
        StringBuilder sumFactor = new StringBuilder();

        String[] factors = assessResultDAO.getAssessRiskFactor(tableName);
        int[] scores =  assessResultDAO.getQuestionScore(tableName);
        if(sumScore > 0) {
            for (int i = 0; i < factors.length; i++) {
                if (scores[i] > 0) {
                    String factor = factors[i] + "、";
                    System.out.println(factor);
                    sumFactor.append(factor);
                }
            }
        }else {
            sumFactor.append("无");
        }

        return sumFactor.toString();
    }
}
